package com.ruoyi.gczl.bzgf.ws.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gczl.bzgf.ws.domain.Bzgfws;
import com.ruoyi.gczl.bzgf.ws.mapper.BzgfwsMapper;
import com.ruoyi.gczl.bzgf.ws.service.IBzgfwsService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标准规范无损Service业务层处理
 *
 * @author huacai
 * @date 2024-08-28
 */
@Service
public class BzgfwsServiceImpl implements IBzgfwsService {
    @Resource
    private BzgfwsMapper bzgfwsMapper;

    @Resource
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询标准规范无损
     *
     * @param bzgfwsId 标准规范无损主键
     * @return 标准规范无损
     */
    @Override
    public Bzgfws selectBzgfwsByBzgfwsId(Integer bzgfwsId) {
        return bzgfwsMapper.selectBzgfwsByBzgfwsId(bzgfwsId);
    }

    /**
     * 查询标准规范无损列表
     *
     * @param bzgfws 标准规范无损
     * @return 标准规范无损
     */
    @Override
    @DataScope(deptAlias = "t1")
    public List<Bzgfws> selectBzgfwsList(Bzgfws bzgfws) {
        return bzgfwsMapper.selectBzgfwsList(bzgfws);
    }

    /**
     * 新增标准规范无损
     *
     * @param bzgfws 标准规范无损
     * @return 结果
     */
    @Override
    public int insertBzgfws(Bzgfws bzgfws) {
        bzgfws.setCreateTime(DateUtils.getNowDate());
        return bzgfwsMapper.insertBzgfws(bzgfws);
    }

    /**
     * 批量新增标准规范无损
     *
     * @param bzgfwss 标准规范无损List
     * @return 结果
     */
    @Override
    public int batchInsertBzgfws(List<Bzgfws> bzgfwss) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(bzgfwss)) {
            try {
                for (int i = 0; i < bzgfwss.size(); i++) {
                    int row = bzgfwsMapper.insertBzgfws(bzgfwss.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == bzgfwss.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改标准规范无损
     *
     * @param bzgfws 标准规范无损
     * @return 结果
     */
    @Override
    public int updateBzgfws(Bzgfws bzgfws) {
        bzgfws.setUpdateTime(DateUtils.getNowDate());
        return bzgfwsMapper.updateBzgfws(bzgfws);
    }

    /**
     * 批量删除标准规范无损
     *
     * @param bzgfwsIds 需要删除的标准规范无损主键
     * @return 结果
     */
    @Override
    public int deleteBzgfwsByBzgfwsIds(Integer[] bzgfwsIds) {
        return bzgfwsMapper.deleteBzgfwsByBzgfwsIds(bzgfwsIds);
    }

    /**
     * 删除标准规范无损信息
     *
     * @param bzgfwsId 标准规范无损主键
     * @return 结果
     */
    @Override
    public int deleteBzgfwsByBzgfwsId(Integer bzgfwsId) {
        return bzgfwsMapper.deleteBzgfwsByBzgfwsId(bzgfwsId);
    }
}
